﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDOS.Objects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace DDOS.Game_State
{
    class MainGambitLayer: ScreenLayer
    {
        private bool loading = true;
        private bool unloading= false;
        private Player player;
        private Vector2 menuPos =new Vector2(340,-150);
        private Vector2[] minionBoxes;
        private Vector2[] minions;
        private Texture2D isSelected;
        private Texture2D notSelected;
        private Texture2D isClicked;
        private int pntr;
        private bool[] select;
        private bool[] clicked;

        public MainGambitLayer(Player p)
            :base(true)
        {
            pntr = 0;
            select = new bool[4];
            select[0] = true;
            select[1] = false;
            select[2] = false;
            select[3] = false;
            clicked = new bool[4];
            clicked[0] = false;
            clicked[1] = false;
            clicked[2] = false;
            clicked[3] = false;
            player = p;
            loading = true;
            unloading = false;
            menuPos = new Vector2(340,-150);
            minionBoxes = new Vector2[4];
            minionBoxes[0] = new Vector2(380,-125);
            minionBoxes[1] = new Vector2(520,-125);
            minionBoxes[2] = new Vector2(660,-125);
            minionBoxes[3] = new Vector2(800,-125);
            minions = new Vector2[4];
            /*minions[0] = new Vector2();
            minions[1] = new Vector2();
            minions[2] = new Vector2();
            minions[3] = new Vector2();*/
            isSelected = ImageManager2D.Instance.ButtonSelected;
            notSelected = ImageManager2D.Instance.ButtonNormal;
            isClicked =  ImageManager2D.Instance.ButtonClicked;
        }

        public void OnLoad()
        {
            
            if (menuPos.Y < 0)
            {
               
                menuPos.Y += 5;
                for (int i = 0; i < 4; i++)
                {
                    minionBoxes[i].Y += 5;
                    minions[i].Y += 5;

                }
            }
            else
            {
                loading = false;
            }
        }

        public void OnExit()
        {
            if (menuPos.Y > -150)
            {
                menuPos.Y -= 5;
                for (int i = 0; i < 4; i++)
                {
                    minionBoxes[i].Y -= 5;
                    minions[i].Y -= 5;

                }
            }
            else
            {
                unloading = false;
                ScreenManager.Instance.CurrentScreen.RemoveLayer(this);
            }
        }

        public override void Update(GameTime gt)
        {
            if (loading)
            {
               
                OnLoad();
            }
            else
            {
                if (unloading)
                {
                    OnExit();
                }
                else
                {

                    if (InputManager.InputManagerInstance.KeyWasReleased(Keys.Left) || InputManager.InputManagerInstance.GamePadThumbstickMovedLeft(InputManager.GamePadThumbstick.left) || InputManager.InputManagerInstance.GamePadButtonWasReleased(Buttons.DPadLeft))
                    {
                        clicked[pntr] = false;
                        select[pntr] = false;
                        pntr -= 1;
                        if(pntr <0)
                            pntr = 0;
                        select[pntr] = true;
                    }

                    if (InputManager.InputManagerInstance.KeyWasReleased(Keys.Right) || InputManager.InputManagerInstance.GamePadThumbstickMovedRight(InputManager.GamePadThumbstick.right) || InputManager.InputManagerInstance.GamePadButtonWasReleased(Buttons.DPadRight))
                    {
                        clicked[pntr] = false;
                        select[pntr] = false;
                        pntr += 1;
                        if (pntr > 3)
                            pntr = 3;
                        select[pntr] = true;
                    }
                    if (InputManager.InputManagerInstance.GamePadButtonWasReleased(Buttons.B) ||
                        InputManager.InputManagerInstance.KeyWasReleased(Keys.Escape))
                        unloading = true;

                    if (InputManager.InputManagerInstance.GamePadButtonWasReleased(Buttons.A) || InputManager.InputManagerInstance.KeyWasReleased(Keys.Enter))
                    {
                        clicked[pntr] = true;
                        ScreenManager.Instance.CurrentScreen.PushLayer(new MinionGambitLayer(player.Minions[pntr]));
                    }

                }
            }
        }

        public override void Draw()
        {
            ScreenManager.Instance.CurrentSpriteBatch.Draw(ImageManager2D.Instance.Tray, menuPos, Color.White);
            for (int i = 0; i < 4; i++)
            {
                if (clicked[i])
                {
                    ScreenManager.Instance.CurrentSpriteBatch.Draw(isClicked, minionBoxes[i], Color.White);
                }
                else if (select[i])
                {
                    ScreenManager.Instance.CurrentSpriteBatch.Draw(isSelected, minionBoxes[i], Color.White);
                }
                else
                {
                    ScreenManager.Instance.CurrentSpriteBatch.Draw(notSelected, minionBoxes[i], Color.White);
                }
                
            }
        }
    }
}
